package com.lecture.java8.reflex.reflections;

import com.lecture.java8.reflex.annotation.UdFunction;
import com.greenpineyu.fel.function.CommonFunction;
import org.reflections.Reflections;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author luyn
 * @version 1.0
 * @description 使用双重检测单例模式
 * @date 2022/2/7 15:33
 */
public class UdFunctionHolder {

    private static UdFunctionHolder udFunctionHolder = null;
    private final List<CommonFunction> commonFunctionList;

    private UdFunctionHolder() {
        commonFunctionList = new ArrayList<>();
        //使用反射加注解的形式获取所有的符合条件的类
        Set<Class<?>> classSet = new Reflections("com.lecture.java8.reflex").getTypesAnnotatedWith(UdFunction.class);
        for (Class<?> c : classSet) {
            if (CommonFunction.class.isAssignableFrom(c)) {
                try {
                    commonFunctionList.add((CommonFunction) c.newInstance());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public static UdFunctionHolder getInstance() {
        if (null == udFunctionHolder) {
            synchronized (UdFunctionHolder.class) {
                if (null == udFunctionHolder) {
                    udFunctionHolder = new UdFunctionHolder();
                }
            }
        }
        return udFunctionHolder;
    }

    public List<CommonFunction> getCommonFunctionList() {
        return commonFunctionList;
    }
}
